﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RESTAgent.Hal.Tests.Fakes;
using Tavis;

namespace RESTAgent.Hal.Tests {
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1 {
        [TestMethod]
        public void NavigateToRootAndGetLink() {
            //Arrange

            var halContent =
                new StringContent("<resource rel='self'><link rel='foo' href='http://example.org/link/'/></resource>",
                                  Encoding.UTF8, "application/hal+xml");
            halContent.Headers.ContentType = new MediaTypeHeaderValue("application/hal+xml");
            //plainTextContent.Links.Add("foo", new Link("http://example.org/link/"));
            var httpClient = new HttpClient(new FakeWebRequestChannel(halContent));

            var agent = new RestAgent(httpClient);

            agent.RegisterSemanticsProvider(new HalSemanticsProvider());
  

            agent.NavigateTo(new Link(new Uri("http://example.org/")));

            //Act
            var link = agent.CurrentContent.GetLink("foo");

            //Assert
            Assert.AreEqual("http://example.org/link/", link.Target.ToString());
            Assert.IsNotNull(agent);
        }


        [TestMethod]
        public void NavigateToRootAndNAvigateToLink() {
            //Arrange


            var halText = "<resource rel='self'><link rel='foo' href='http://example.org/link/'/></resource>";
            var halContent = new StringContent(halText, Encoding.UTF8, "application/hal+xml");
            var fakeWebRequestChannel = new FakeWebRequestChannel(halContent);
            var httpClient = new HttpClient(fakeWebRequestChannel);


            var agent = new RestAgent(httpClient);

            agent.RegisterSemanticsProvider(new HalSemanticsProvider());
  
            agent.NavigateTo(new Link(new Uri("http://example.org/")));

            //Act
            var link = agent.CurrentContent.GetLink("foo");

            fakeWebRequestChannel.Content = new StringContent(halText, Encoding.UTF8, "application/hal+xml");

            agent.NavigateTo(link);

            //Assert
            Assert.AreEqual("http://example.org/link/", link.Target.ToString());
            Assert.IsNotNull(agent);
        }
    }
}
